{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 2.8 矢量化字符串"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:19.469761Z",
     "end_time": "2024-05-09T20:54:19.967404Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "'Newton'"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name = \"newton\"\n",
    "name.capitalize()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:19.972351Z",
     "end_time": "2024-05-09T20:54:20.028385Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "['Newton', 'Hertz', 'Curie']"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = ['newton', 'hertz', 'curie']\n",
    "[name.capitalize() for name in names]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:19.996968Z",
     "end_time": "2024-05-09T20:54:20.028385Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "0    6.0\n1    7.0\n2    NaN\n3    9.0\ndtype: float64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = [1, 2, None, 4]\n",
    "ns = pd.Series(n)  #转化为NumPy的数组亦然\n",
    "ns + 5"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.012760Z",
     "end_time": "2024-05-09T20:54:20.028385Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "0    newton\n1     hertz\n2      None\n3     curie\ndtype: object"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names2 = ['newton', 'hertz', None, 'curie']\n",
    "snames = pd.Series(names2)\n",
    "snames"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.028385Z",
     "end_time": "2024-05-09T20:54:20.046006Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "0    Newton\n1     Hertz\n2      None\n3     Curie\ndtype: object"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "snames.str.capitalize()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.048652Z",
     "end_time": "2024-05-09T20:54:20.109646Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "(False, True)"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"str\" in dir(ns), \"str\" in dir(snames)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.068696Z",
     "end_time": "2024-05-09T20:54:20.151457Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "array(['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',\n       'find', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',\n       'islower', 'isnumeric', 'isspace', 'istitle', 'isupper', 'join',\n       'ljust', 'lower', 'lstrip', 'partition', 'removeprefix',\n       'removesuffix', 'replace', 'rfind', 'rindex', 'rjust',\n       'rpartition', 'rsplit', 'rstrip', 'split', 'startswith', 'strip',\n       'swapcase', 'title', 'translate', 'upper', 'zfill'], dtype='<U12')"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = dir(str)\n",
    "b = dir(snames.str)\n",
    "da = [i for i in a if '_' not in i]\n",
    "db = [i for i in b if '_' not in i]\n",
    "share = np.intersect1d(da, db)\n",
    "share"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.086470Z",
     "end_time": "2024-05-09T20:54:20.151457Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "0       issac newton\n1    albert einstein\n2    max karl planck\n3    wilhelm röntgen\ndtype: object"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy = ['issac newton', 'albert Einstein', 'Max Karl Planck', 'Wilhelm Röntgen']\n",
    "phy = pd.Series(phy)\n",
    "phy.str.lower()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.093815Z",
     "end_time": "2024-05-09T20:54:20.151457Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "0    12\n1    15\n2    15\n3    15\ndtype: int64"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy.str.len()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.113906Z",
     "end_time": "2024-05-09T20:54:20.151457Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "0        [issac, newton]\n1     [albert, Einstein]\n2    [Max, Karl, Planck]\n3     [Wilhelm, Röntgen]\ndtype: object"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy.str.split()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.130821Z",
     "end_time": "2024-05-09T20:54:20.202553Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "issac newton       new@itdiffer.com\nalbert Einstein    ein@itdiffer.com\nMax Karl Planck    pla@itdiffer.com\nWilhelm Röntgen    ron@itdiffer.com\nlaoqi                           NaN\ndtype: object"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy_mail = pd.Series(['new@itdiffer.com', 'ein@itdiffer.com', 'pla@itdiffer.com', 'ron@itdiffer.com'], index=phy)\n",
    "phy_mail['laoqi'] = np.NaN\n",
    "phy_mail"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.151457Z",
     "end_time": "2024-05-09T20:54:20.206562Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "issac newton       True\nalbert Einstein    True\nMax Karl Planck    True\nWilhelm Röntgen    True\nlaoqi               NaN\ndtype: object"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "email_str = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,4}$'\n",
    "phy_mail.str.match(email_str)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.172730Z",
     "end_time": "2024-05-09T20:54:20.206562Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "0       issac newton\n1    albert Einstein\n2    Max Karl Planck\n3    Wilhelm Röntgen\ndtype: object"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.182683Z",
     "end_time": "2024-05-09T20:54:20.206562Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "0    s\n1    b\n2    x\n3    l\ndtype: object"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy.str[2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.186855Z",
     "end_time": "2024-05-09T20:54:20.206562Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "0    s\n1    b\n2    x\n3    l\ndtype: object"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy.str.get(2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.206562Z",
     "end_time": "2024-05-09T20:54:20.223000Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "0    issac\n1    alber\n2    Max K\n3    Wilhe\ndtype: object"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy.str[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.226771Z",
     "end_time": "2024-05-09T20:54:20.270589Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "0    issac\n1    alber\n2    Max K\n3    Wilhe\ndtype: object"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phy.str.slice(0, 5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.246772Z",
     "end_time": "2024-05-09T20:54:20.270589Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:54:20.254907Z",
     "end_time": "2024-05-09T20:54:20.270589Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
